/*		<one line to give the program's name and a brief idea of what it does.>
 * Copyright (C) <2025>  <zhen>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 * */

#ifndef X_LIST_H
#define X_LIST_H

#include <stdint.h>

struct X_list
{
    void *prev;
    void *next;
};

#define XListInit(type, self, list)    \
    do                                 \
    {                                  \
        ((type)self)->list.prev = self; \
        ((type)self)->list.next = self; \
    } while (0)

#define XListRem(type, self, list)                                                \
    do                                                                            \
    {                                                                             \
        if ((type)self->list.prev == self)                                        \
        {                                                                         \
            break;                                                                \
        }                                                                         \
        else                                                                      \
        {                                                                         \
            if ((type)self->list.next == self)                                    \
            {                                                                     \
                ((type)((type)self->list.prev))->list.next = (type)self->list.prev; \
            }                                                                     \
            else                                                                  \
            {                                                                     \
                ((type)((type)self->list.prev))->list.next = (type)self->list.next; \
            }                                                                     \
        }                                                                         \
    } while (0)

#endif // X_LIST_H
